﻿using Ninject;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Moq;
using MobilesStore.Domain.Abstract;
using MobilesStore.Domain.Entities;
using MobilesStore.Domain.Concrete;
using System.Configuration;
using MobilesStore.WebUI.InfraStructure.Abstract;
using MobilesStore.WebUI.InfraStructure.Concrete;

namespace MobilesStore.WebUI.InfraStructure
{
    public class NinjectControllerFactory:DefaultControllerFactory
    {
        private IKernel NinjectKernel;

        protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, Type controllerType)
        {
            return controllerType == null ? null : (IController)NinjectKernel.Get(controllerType);
        }

        public NinjectControllerFactory()
        {
            NinjectKernel = new StandardKernel();
            Addbinding();
        }

        private void Addbinding()
        {
           //set up mock
            /*Mock<IProductsRepository> mock = new Mock<IProductsRepository>();
            mock.Setup(e => e.Products).Returns(new List<Product>
            {
                new Product{ProductName="Sam Sung Galaxy S4",ProductPrice=10000000},
                new Product{ProductName="Nokia N97",ProductPrice=5000000},
                new Product{ProductName="Sony K300",ProductPrice=2000000}
            }.AsQueryable());*/

            //add binding

            EmailSettings emailSettings = new EmailSettings {
                WriteAsFile = bool.Parse(ConfigurationManager
                .AppSettings["Email.WriteAsFile"] ?? "false")
            };

            NinjectKernel.Bind<IOrderProcessor>()
                        .To<EmailOrderProcessor>()
                        .WithConstructorArgument("settings", emailSettings);

           NinjectKernel.Bind<IProductsRepository>().To<EFProductRepository>();

           NinjectKernel.Bind<IAuthProvider>().To<FormAuthProvider>();
           NinjectKernel.Bind<ICommentRepository>().To<EFCommentRepository>();
        }
    }
}